//
// Created by mi on 2024/1/28.
//
#include "message.pb.h"
#include <memory>
#include <google/protobuf/util/json_util.h>

using namespace std;

int main() {
    std::unique_ptr<MessageData> msg = std::make_unique<MessageData>();
    CommonMessageHeader header = msg->message_header();
    header.set_msg_id(100);
    header.set_msg_type(CommonMessageHeader_MessageType::CommonMessageHeader_MessageType_QUERY);
    header.set_msg_size(1000);
    header.set_timestamp(std::time(nullptr));
    //这种方式需要你自己开辟对象内存空间。
    //msg->set_allocated_add_student_list(AddStudentList *add)
    //这种方式会内存开辟空间
    AddStudentList *list = msg->mutable_add_student_list();
    for (int i = 0; i < 5; i++) {
        AddStudentList_AddStudent *student = list->add_student_list();
        student->set_age(i);
        student->set_address("china");
        student->set_id(i);
        student->set_age(i);
        student->set_name("yezongwen" + std::to_string(i));
    }
    std::string out{};
    msg->SerializeToString(&out);
    MessageData addMsg;
    addMsg.ParseFromString(out);
    out.clear();
    google::protobuf::util::MessageToJsonString(addMsg, &out);
    std::cout << out << std::endl;
    return 0;
}